Ext.define('Avrio.view.Session', {
    extend: 'Ext.grid.Panel',

    alias: 'widget.session',

    title: 'Session Table',
    store: 'Session',

    frame: true,
    columnLines: true,
    selType: 'cellmodel',

    initComponent: function() {
        var me = this; // me == grid

        me.rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToMoveEditor: 1,
            autoCancel: false
        });
        Ext.applyIf(me, {
            height: 500,
            plugins: [me.rowEditing], //me.editing, me.rowEditing
            viewConfig: { stripeRows: true },
//<editor-fold defaultstate="collapsed" desc="columns">
            columns: [
                { xtype: 'rownumberer' },
                //{ xtype: 'gridcolumn', header: 'ID', dataIndex: 'id', width: 100 },
                {
                    xtype: 'gridcolumn', header: 'Version', dataIndex: 'version', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Type Code', dataIndex: 'session_type_code', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Status Code', dataIndex: 'status_code', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'datecolumn', header: 'Start Time', dataIndex: 'session_start_time', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: { 
                        xtype: 'datefield',
                        format: 'm/d/y',
                        maxValue: Ext.Date.format(new Date(), 'm/d/Y')
                    }
                }, {
                    xtype: 'datecolumn', header: 'End Time', dataIndex: 'session_end_time', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: { 
                        xtype: 'datefield',
                        format: 'm/d/y',
                        maxValue: Ext.Date.format(new Date(), 'm/d/Y')
                    }
                }, {
                    xtype: 'gridcolumn', header: 'Confidentiality Code', dataIndex: 'confidentiality_code', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Priority Code', dataIndex: 'priority_code', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Reason', dataIndex: 'reason', width: 150, align: 'right', sortable: false, menuDisabled: false,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Scheduled by user id', dataIndex: 'scheduled_by_user_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Patient id', dataIndex: 'patient_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Referring Practitioner id', dataIndex: 'referring_practitioner_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Referring Practitioner Participation Role', dataIndex: 'referring_practitioner_participation_role', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Referring Practitioner Session Time', dataIndex: 'referring_practitioner_session_time', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Referring Practitioner Pretime', dataIndex: 'referring_practitioner_pretime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Referring Practitioner Posttime', dataIndex: 'referring_practitioner_posttime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant 1', dataIndex: 'consultant1_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant 2', dataIndex: 'consultant2_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant1 Participation Role', dataIndex: 'consultant1_participation_role', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant2 Participation Role', dataIndex: 'consultant2_participation_role', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant1 Session Time', dataIndex: 'consultant1_session_time', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant1 Pretime', dataIndex: 'consultant1_pretime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant1 Posttime', dataIndex: 'consultant1_posttime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant2 Session Time', dataIndex: 'consultant2_session_time', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant2 Pretime', dataIndex: 'consultant2_pretime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Consultant2 Posttime', dataIndex: 'consultant2_posttime', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Cause of Session id', dataIndex: 'cause_of_session_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Caused by Session id', dataIndex: 'caused_by_session_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Responsible Organization id', dataIndex: 'responsible_organization_id', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }, {
                    xtype: 'gridcolumn', header: 'Language Code', dataIndex: 'language_code', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Bit Rate', dataIndex: 'bit_rate', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield' }
                }
            ],
// </editor-fold>
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: 'Session',
                dock: 'bottom',
                displayInfo: true,
                items: [' ',{
                    text: 'Add Session',
                    handler: function() {
                        me.rowEditing.cancelEdit();

                        var rec = Ext.create('Avrio.model.Session',{
                            session_start_time: new Date(),
                            session_end_time: new Date()
                        });

                        var store = me.getView().getStore();
                        store.insert(0, rec);
                        
                        me.rowEditing.startEdit(0, 0);
                    }
                }]
            }],
            listeners: {
                render: function(grid) {
                    grid.getStore().load();
                }
            }
        });
        me.callParent(arguments);
    }
});
